package com.uinnova.springboot.asynchronization;

import org.checkerframework.checker.units.qual.A;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

@Service
public class MsgService {
    @Async
    public void sendA() throws Exception{
        System.out.println("send A");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(5000);
        Long endTime = System.currentTimeMillis();
        System.out.println("sendA耗时："+(endTime-startTime));
    }

    @Async
    public void sendB() throws  Exception{
        System.out.println("send B");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("sendb耗时："+(endTime-startTime));
    }

    @Async
    public Future<String> sendC() throws Exception {
        System.out.println("send C");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("sendC耗时：" + (endTime - startTime));
        return new AsyncResult<String>("sendC");
    }

    @Async
    public Future<String> sendD() throws  Exception{
        System.out.println("send D");
        Long startTime = System.currentTimeMillis();
        Thread.sleep(3000);
        Long endTime = System.currentTimeMillis();
        System.out.println("sendD耗时：" + (endTime - startTime));
        return new AsyncResult<String>("sendD");
    }

    public void sync() throws  InterruptedException{
        System.out.println("sync-累了-休息一会");
        Thread.sleep(5000);
        System.out.println("同步休息五秒之后，当前方法执行");
    }

    public void syncAfter(){
        System.out.println("同步操作休息好了,该上场了");
    }

    /**
     * 测试异步
     */

    @Async
    public void async() throws InterruptedException {
        System.out.println("async-异步-累了-休息一会");
        Thread.sleep(5000);
        System.out.println("async-休息五秒之后，执行");

    }

    @Async
    public void asyncAfter(){
        System.out.println("异步操作，我不用等待,该我上场了");
    }


}
